using System;
using Drill.Configuration.ConfigFile;
using Drill.Core.Utilities;

namespace Drill.Configuration
{
    /// <summary>
    /// Loads dependency resolution configuration data from configuration files.
    /// </summary>
    public static class ConfigurationLoader
    {
        /// <summary>
		/// Loads all dependency resolver configuration data from the default configuration file.
        /// </summary>
        /// <returns>An instance of <see cref="IDependencyResolverConfiguration"/> or null if the configuration data could not be loaded.</returns>
		public static IDependencyResolverConfigurationCollection LoadAllDependencyResolverConfigs()
        {
			string configSectionName = BaseConfigurationSection.GetConfigSectionName<DependencyResolversConfigurationSection>();
			IDependencyResolversConfiguration config = (DependencyResolversConfigurationSection)ConfigurationUtility.GetConfigSectionFromConfigFile(configSectionName);
	        return config.DependencyResolvers;
        }

		/// <summary>
		/// Loads all dependency resolver configuration data from the specified configuration file.
		/// </summary>
		/// <param name="filename">The filename of the configuration file from which the dependency services configuration data will be loaded.</param>
		/// <returns>An instance of <see cref="IDependencyResolverConfiguration"/> or null if the configuration data could not be loaded.</returns>
		public static IDependencyResolverConfigurationCollection LoadAllDependencyResolverConfigs(string filename)
		{
			string configSectionName = BaseConfigurationSection.GetConfigSectionName<DependencyResolversConfigurationSection>();
			IDependencyResolversConfiguration config = (DependencyResolversConfigurationSection)ConfigurationUtility.GetConfigSectionFromConfigFile(configSectionName, filename);
			return config.DependencyResolvers;
		}

		/// <summary>
		/// Loads the specified dependency resolver configuration data from the default configuration file.
		/// </summary>
		/// <param name="name">The name of the dependency resolver configuration to load.</param>
		/// <returns>
		/// An instance of <see cref="IDependencyResolverConfiguration" /> or null if the configuration data could not be loaded.
		/// </returns>
		/// <exception cref="System.ArgumentNullException">name</exception>
		public static IDependencyResolverConfiguration LoadDependencyResolverConfig(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				throw new ArgumentNullException("name");
			}

			IDependencyResolverConfigurationCollection configs = LoadAllDependencyResolverConfigs();
			return configs[name];
		}

		/// <summary>
		/// Loads the specified dependency resolver configuration data from the specified configuration file.
		/// </summary>
		/// <param name="name">The name of the dependency resolver configuration to load.</param>
		/// <param name="filename">The filename of the configuration file from which the dependency services configuration data will be loaded.</param>
		/// <returns>
		/// An instance of <see cref="IDependencyResolverConfiguration" /> or null if the configuration data could not be loaded.
		/// </returns>
		public static IDependencyResolverConfiguration LoadDependencyResolverConfig(string name, string filename)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				throw new ArgumentNullException("name");
			}

			IDependencyResolverConfigurationCollection configs = LoadAllDependencyResolverConfigs(filename);
			return configs[name];
		}
    }
}